<?php
include_once "../../qc_base.php";
include_once "../../qc_utils.php";
include_once "../../bootstrap.php";

include_once "../../classes/MakePage.php";


function makesuredir($dir){
	if(!file_exists(dirname($dir))){
		makesuredir(dirname($dir));
	}
	if(!file_exists($dir)){
		mkdir($dir);
	}
}

function write_pdost_view_to_file($crudset_id){

	$qcs = Doctrine_Query::create()
	->from('Crudsets cs')->where('crudset_id = ?',$crudset_id);
	$setinfo = $qcs->fetchOne();

	// get the main table for the set
	$qtb = Doctrine_Query::create()
	->from('Tablesources tb')->where('tablesource_id = ?',$setinfo['main_table']);
	$tablesource = $qtb->fetchOne();
	$table_name = $tablesource['table_name'];

	$q = Doctrine_Query::create()
	->from('Projects p')->where('project_id = ?',$setinfo['project_id']);
	$project = $q->execute()->getFirst();	
	
	$project_db = $project->project_db;
	$project_path = $project->project_path;
	$project_server = $project->project_server;
	$project_login = $project->project_login;
	$project_pass = $project->project_pass;
   	$project_name = $project->project_name;
	$class_subpath = $project->class_subpath;
	$template_dir = $project->template_dir;	
	$structure = $project->file_structure;

	$structure_path = QC_SYS_BASEDIR . '/structures/';
	
	include $structure_path . $structure .'/paths.php';
	

	$outpath = $paths['modules'];
	$outpath = str_replace('<setname>',$setinfo['cs_name'],$outpath);
	
	$outpath = QC_SYS_BASEDIR . '/' . QC_PROJECT_SUBPATH . $project_name . '/' . $outpath ;

	makesuredir($outpath);
	
	
	$targetfile = $outpath . $setinfo['cs_name'] . '_view.php';
	$tplfile = 'pdost_view_tpl.php';
	echo 'OUTPUT TO: ' . $targetfile;

//--------------------Create Replacement Text



$setcsname = '
// SET THE NAME 
'."\$name = '" . $setinfo['cs_name'] . "';";

$FilterClass = '
$flt = new CF_FilterQueryBase($' . $setinfo['cs_name'] . '_spec);
';

$SorterClass = '
$sorter = new CF_Sorter($' . $setinfo['cs_name'] . '_spec, $name);
';

$SetQuery = '
$qry_list = "SELECT * FROM ' . $table_name . ' WHERE 1  " ;
';

$ApplyFilters = '
$qry_list .=  $flt->make_query_PSfilter();
';
$ApplySort = '
$qry_list .= $sorter->make_query_sort();	
';

$BindFilters = '
	$flt->make_PSfilter_bindings($stmt);
';

$ArraySpec = '
	$arrayBySpec = new ' . $setinfo['cs_name'] . 'Array($list,$' . $setinfo['cs_name'] . '_spec);
	$list = $arrayBySpec->fill_spec_array();
';

$Lookup = '
	$' . $setinfo['cs_name'] . 'Lookups = new ' . $setinfo['cs_name'] . 'LookupList();
	$' . $setinfo['cs_name'] . 'Lookups->SetFilter($Filters);
	$' . $setinfo['cs_name'] . 'Lookups->ResolveSpecLists($' . $setinfo['cs_name'] . '_spec);
';

$FilterBox = '
	$filterbox = new CF_FilterFormFull($' . $setinfo['cs_name'] . '_spec,$Filters,$_SERVER[\'PHP_SELF\']);
';

$Heading = '
	$heading = new CF_Header($' . $setinfo['cs_name'] . '_spec,$_SERVER[\'PHP_SELF\']);
';

$TableClass = '
class ' . $setinfo['cs_name'] . 'Table extends CF_TableAjax {
';

$CloseClass = '
}
';

$TableSetup = '
$table = new ' . $setinfo['cs_name'] . 'Table($' . $setinfo['cs_name'] . '_spec,$none,\'' . $setinfo['cs_name'] . '\');
$table->set_valuesarray($list);	
$table->actionpage = \'' . $setinfo['cs_name'] . '/' . $setinfo['cs_name'] . '_proc.php\';
//$table->set_lookup(\'reportsTo\',$' . $setinfo['cs_name'] . 'Lookups->lookup_reportsTo());
';

$MultiAct = '
$multiact = new CF_Multiselect($' . $setinfo['cs_name'] . '_spec, $name, \'' . $setinfo['cs_name'] . '/' . $setinfo['cs_name'] . '_proc.php\');
';

//---------------------------Write into file	
	
	

	$pagemaker = new MakePage();


	$pagemaker->AddReplacement('setcsname',$setcsname);	
	$pagemaker->AddReplacement('FilterClass',$FilterClass);	
	$pagemaker->AddReplacement('SorterClass',$SorterClass);
	$pagemaker->AddReplacement('SetQuery',$SetQuery);
	$pagemaker->AddReplacement('ApplyFilters',$ApplyFilters);			
	$pagemaker->AddReplacement('ApplySort',$ApplySort);
	$pagemaker->AddReplacement('BindFilters',$BindFilters);
	$pagemaker->AddReplacement('ArraySpec',$ArraySpec);
	$pagemaker->AddReplacement('Lookup',$Lookup);
	$pagemaker->AddReplacement('FilterBox',$FilterBox);
	$pagemaker->AddReplacement('Heading',$Heading);	
	$pagemaker->AddReplacement('TableClass',$TableClass);
	$pagemaker->AddReplacement('CloseClass',$CloseClass);
		
	$pagemaker->AddReplacement('TableSetup',$TableSetup);
	$pagemaker->AddReplacement('MultiAct',$MultiAct);	
	
	$newstuff = $pagemaker->ReplaceInFile($tplfile,$targetfile);
	
	$pagemaker->PrintReplacementsTable();

	echo '<pre>' . $pagemaker->str_log . '</pre>';
	
	file_put_contents($targetfile,$newstuff);	

	return $newstuff;


}
?>
